package com.woniuxy.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.pojo.Record;
import com.woniuxy.pojo.Rewards;
import com.woniuxy.pojo.Staff;
import com.woniuxy.pojo.Work;
import com.woniuxy.service.RewardsService;
import com.woniuxy.service.StaffService;

@Controller
@RequestMapping("/rewards")
public class RecordsControlller {
	 @Autowired
     private RewardsService rewardsService;
	 @Autowired
		private StaffService staffService;
     
     @RequestMapping("/rewardsList")
     public String getAll(@RequestParam(required=false,defaultValue="1")Integer startPage,@RequestParam(required = false, defaultValue = "5") Integer PageSize,Model model,HttpServletRequest req){
 		Integer staffId = null;
 		if (req.getParameter("serch_id")!=null&&req.getParameter("serch_id")!="") {
 			staffId = Integer.parseInt(req.getParameter("serch_id"));
 		}	
 		PageHelper.startPage(startPage, PageSize);
 		List<Rewards> rewards = rewardsService.getAll(staffId);
 		List<Staff> allStaff = staffService.getAllStaff();
 		model.addAttribute("staffList",allStaff);
 		PageInfo<Rewards> pageData = new PageInfo<Rewards>(rewards);
 		System.out.println("sssssss"+pageData);
 		model.addAttribute("rewards",pageData);
 		
 		
 		return "forward:/staffInfo/rewards.jsp";
 	}

 	@RequestMapping("/addWho")
 	public String addWho(Model model){
 		List<Staff> allStaff = staffService.getAllStaff();
 		model.addAttribute("staffList",allStaff);
 		return "forward:/staffInfo/rewards-add.jsp";
 	}
 	
  @RequestMapping("/addRewards")
  public String addRewards(Rewards rewards){
	  rewardsService.addRewards(rewards);
  	return "redirect:/rewards/rewardsList";	
  }
  
  @ResponseBody
	@RequestMapping("/deleteMany")
	public boolean deleteMany(String ids){
		int rows =rewardsService.deleteManyByIds(ids);
		if (rows!=0) {
			return true;
		}else{
			return false;
		}
				
	}
  @RequestMapping("/delRewards")
  public String delRewards(Integer id){
      System.out.println("id=="+id);  	
      rewardsService.delRewards(id);
    	return "redirect:/rewards/rewardsList";	
  }
  
  @RequestMapping("/updateOne")
	public String updateOne(Model model,Integer cid){
		Rewards rewards =rewardsService.getRewardsByCid(cid);
		List<Staff> allStaff = staffService.getAllStaff();
		model.addAttribute("staffList",allStaff);

		model.addAttribute("rewards",rewards);
		return "forward:/staffInfo/rewards_update.jsp";
	}
	
	@RequestMapping("/saveUpdate")
	public String saveUpdate(Rewards rewards){
		rewardsService.saveUpdate(rewards);
		return "redirect:/rewards/rewardsList";
	}
 

}
